<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Org\Upload;
use App\Org\Image;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
       
       $db=DB::table('order');
       //判断并封装搜索条件
       $params = array();
       if(!empty($_GET['name'])){
           $db->where("bid","like","%{$_GET['name']}%");

           $params['name'] = $_GET['name']; //维持搜索条件
       }

        $list=$db->where('recyle','=',"1")
                 ->paginate(5);
                               
        return view('admin.order.index',['list'=>$list,
                                         'params'=>$params   
                                                    ]);

    }
       

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    //加载回收站页面
    public function create()
    {   
        $list=DB::table('order')->where('recyle','=',"0")->paginate(5);
        return view('admin.order.recyle',['list'=>$list]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $input=$request->all();
        $list=DB::table('order')->where('id',$input['id'])->first();
        
        return view("admin.order.edit",['list'=>$list]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //翻入回收站
    public function show($id)
    {   
        
        $db=DB::table('order');
      
        $m=DB::table('order')
            ->where('id', $id)
            ->update(['recyle' => 0]);
        $list=$db->where('recyle','=','0')->paginate(5);
        
        if($m>0){
            return view('admin.order.recyle',['list'=>$list]);   
        }else{
            return back()->with('err','请重新放入回收站！');
        }
         
  
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    
    //执行还原
    public function edit($id)
    {
       $params=array();
        $m=DB::table('order')
            ->where('id', $id)
            ->update(['recyle' => 1]);
        $list=DB::table('order')->where('recyle','=','1')->paginate(5);
        
        if($m>0){
            return view('admin.order.index',['list'=>$list,
                                                'params'=>$params
                                                ]);   
        }else{
            return back()->with('err','请重新放入回收站！');
        }

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
   
    public function update(Request $request, $id)
    {  
        //表单验证
        //表单验证
        $this->validate($request, [
        "unit"=>'required',
        ],[
        "unit.required"=>"商品的单价不能为空！",
        ]);

        $data=$request->all();
        unset($data['_token']);
        unset($data['_method']);
        
        $m=DB::table('order')
            ->where('id', $id)
            ->update(['unit' => $data['unit'] ]);
      
      if($m>0){
            return redirect('admin/order');
        }else{

            return back()->with('err',"修改失败，请重新修改！");
        } 
       

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //执行回收站删除
    public function destroy($id)
    { 
        $m=DB::table('order')->where('id',"$id")->delete();
        if($m>0){
            return back()->with('err','删除成功！');
        }else{
            return back()->with('err',"是删除失败,请重新删除！");
        }
        
        
    }
}
